//app.js
App({
  cart: [],
  // totalcount:'',
  // totalchecked:[],
  checked1:[],
  totalcheckedcount1:  0,
  totalcheckedprice1: 0,
  onLaunch(){
    this.cart=wx.getStorageSync('cart1') || [];
    // []或者条件
    this.setBadge();
    // this.setStorageSync()
    // this.totalcount = this.cart.reduce((result, item) => {
    //   result += item.count
    //   return result
    // }, 0);
    // console.log(this.totalcount)
    // this.checked()
    // console.log(this.totalchecked)
    // this.checkedcount()
    // this.totalcheckedcount = this.totalchecked.reduce((result, item) => {
    //   result += item.count
    //   return result
    // }, 0);
  },
  // checkedcount() {
  //   this.totalcheckedcount = this.totalchecked.reduce((result, item) => {
  //     result += item.count
  //     return result
  //   }, 0);
  // },
  // cart: []
  // 选择
  checked(item){
    // 点击的时候做两件事一个是计算选择的数量一个是计算选择的总价
    // const checked=[]
    // console.log(this.checked)
    // this.checked = []
    // console.log(item)

    this.checked1.push(item)
  console.log(this.checked1)
    // this.totalchecked.push(item)
    // return this.totalchecked.splice(0, 1)
    // this.totalchecked = this.totalchecked.splice(0,1)
    // console.log()
    // console.log(this.totalchecked)
  },



  totalcheckedcount (){
    this.totalcheckedcount1 = this.checked1.reduce((result, item) => {
      result += item.count
      return result
    }, 0);
    console.log(this.totalcheckedcount1)

  },



  totalcheckedprice(){
    this.totalcheckedprice1 = this.checked1.reduce((result, item) => {
      result += item.count*item.price
      return result
    }, 0);
    console.log(this.totalcheckedprice1)
  },


  addtocart(cartitem){
  const isincart=this.cart.some(item=>item.id===cartitem.id);
  if(isincart){
    this.cart=this.cart.map(item=>{
      if(item.id===cartitem.id){
        item.count+=1
      }
      return item
    })
  }else{
    this.cart=this.cart.concat({
      ...cartitem,
      checked:false,
      count:1
    })
  }
  // console.log(this.cart)
    this.synccarttostorage()
    this.setBadge()
    // this.removestorage()
  },
  checkchange(){
console.log(1)
  },


  // 同步到storage中
  synccarttostorage(){
    wx.setStorageSync('cart1', this.cart)
  },
  // removestorage(){
  //   if(this.totalcount===0){
  //     wx.removeStorageSync('cart1')
  //   }
  // },
    inc(id){
      this.cart=this.cart.map(item=>{
        if(item.id===id){
          item.count+=1
        }
        return item
      })
      this.synccarttostorage()
      this.setBadge()
    },
    red(id){
       this.cart=this.cart.map(item=>{
        if(item.id===id&&item.count>=1){
          item.count-=1
          // if(item.count===0){
          //   this.removestorage()
          // }
        }
        return item
      })

      this.setBadge()
    },
    setBadge(){
      // console.log(this.cart)
      const text=this.cart.reduce((result,item)=>{
        result += item.count
        return result
      }, 0).toString()
      // console.log(text)
      wx.setTabBarBadge({
        index: 2,
        text,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
      // this.setData({
      //   totalcount:text
      // })
      // console.log(this.totalcount)
    }
})